;;; init-keybinds.el --- Description -*- lexical-binding: t; -*-
;;
;; Copyright (C) 2023 gotpl
;;
;; Author: gotpl <631707329@qq.com>
;; Maintainer: gotpl <631707329@qq.com>
;; Created: 七月 27, 2023
;; Modified: 七月 27, 2023
;; Version: 0.0.1
;; Keywords: abbrev bib c calendar comm convenience data docs emulations extensions faces files frames games hardware help hypermedia i18n internal languages lisp local maint mail matching mouse multimedia news outlines processes terminals tex tools unix vc wp
;; Homepage: https://github.com/gotpl/keybinds
;; Package-Requires: ((emacs "24.3"))
;;
;; This file is not part of GNU Emacs.
;;
;;; Commentary:
;;
;;  Description
;;
;;; Code:

;; 放到init-keybinds中会导致evil覆盖一些自定义的快捷键

(global-set-key (kbd "M-o") 'projectile-find-other-file)
(global-set-key (kbd "M-m") nil)
(with-eval-after-load 'evil

  (evil-define-key 'insert 'evil-insert-state-map
    (kbd "<C-return>") 'evil-open-below)


  (evil-define-key 'normal 'global
        "s" nil
        ;; goto
        ;; "f" 'avy-goto-char-timer

        ;; window
        "sv" 'evil-window-vsplit
        "sh" 'evil-window-split
        "so" 'delete-other-windows
        "sc" 'delete-window
        "sC" 'kill-buffer-and-window
        "s=" 'balance-windows)

  (define-leader-key 'normal 'global :localleader
    "" '(:wk "localleader")
    "c" '(:wk "config")
    "cf" 'open-config
    "g" 'magit-status
    "d" 'dired
    )

  (define-leader-key 'normal 'global nil
        ;; SPC, quit minibuffer.
                    ;; "SPC" 'execute-extended-command

        ;; Resume
        "'" 'vertico-repeat

        ;; file
        "f"  '(:wk "files")
        "ff" 'find-file
        "fp" 'project-find-file
        "fa" 'find-files-in-config-dir
        "fF" 'find-file-other-window
        "f/" 'find-file-other-window
        "fC" '+copy-current-file
        "fD" '+delete-current-file
        "fy" '+copy-current-filename
        "fR" '+rename-current-file
        "fr" 'recentf-open-files
        "fl" 'consult-locate
        "fs" 'save-buffer


        ;; buffer & bookmark
        "b" '(:wk "bufmark")
        "bb" 'consult-buffer
        "bp" 'consult-project-buffer
        "bB" 'consult-buffer-other-window
        "bc" 'clone-indirect-buffer
        "bC" 'clone-indirect-buffer-other-window
        "bk" 'kill-current-buffer
        "by" '+copy-current-buffer-name
        "bv" 'revert-buffer-quick
        "bx" 'scratch-buffer
        "bz" 'bury-buffer
        ;; --------------
        "bm" 'bookmark-set
        "bM" 'bookmark-set-no-overwrite
        "bi" 'bookmark-insert
        "br" 'bookmark-rename
        "bd" 'bookmark-delete
        "bw" 'bookmark-write
        "bj" 'bookmark-jump
        "bJ" 'bookmark-jump-other-window
        "bl" 'bookmark-bmenu-list
        "bs" 'bookmark-save

        ;; code
        "c" '(:wk "code")
        "cd" 'rmsbolt-compile
        "cc" 'compile
        "cC" 'recompile
        "ck" 'kill-compilation
        "cw" 'delete-trailing-whitespace
        "cx" 'quickrun
        "cf" 'format-all-region-or-buffer

        ;; window
        "w" 'evil-window-map
        "ww" 'other-frame
        "wd" 'delete-frame
        "wn" 'make-frame
        "wx" 'kill-buffer-and-window
        "wu" '+transient-tab-bar-history
        "w-" 'split-window-vertically
        "w/" 'split-window-horizontally

        "h" '(:wk "help")
        "hf" 'helpful-callable
        "hv" 'helpful-variable
        "hk" 'helpful-key
        "hx" 'helpful-command

        ;; jump
        "jl" 'evil-collection-consult-jump-list
        "jj" 'avy-goto-char-timer
        "jr" 'avy-resume

        ;; ;; tab
        ;; "t" '(:wk "tab")
        ;; "t9" 'tab-bar-switch-to-last-tab
        ;; "tc" 'tab-bar-close-tab
        ;; "tC" 'tab-bar-close-group-tabs
        ;; "tg" 'tab-bar-change-tab-group
        ;; "ti" 'tab-switcher
        ;; "tn" 'tab-bar-new-tab
        ;; "to" 'tab-bar-close-other-tabs
        ;; "tt" 'tab-bar-switch-to-tab
        ;; "tp" 'tab-bar-switch-to-recent-tab
        ;; "tr" 'tab-bar-rename-tab

        ;; search
        "s" '(:wk "search")
        "sj" 'evil-show-jumps
        "sm" 'evil-show-marks
        "sr" 'evil-show-registers
        "si" 'imenu
        "sp" 'consult-ripgrep
        "ss" 'consult-line
        ;; "sf" 'projectile-find-file

        ;; project
        "p" 'projectile-command-map

        ;; app
        "a" '(:wk "app")
        "aa" 'org-agenda
        "ab" 'ebib
        "ac" 'calendar
        "ag" 'gnus
        "ai" 'rcirc

        ;; open
        "o" '(:wk "open")
        "oc" 'org-capture
        "ol" 'org-store-link
        "ot" 'vterm
        "oe" 'eshell
        "os" 'shell

        ;; org
        "oo" '(:wk "org")
        "ool" 'org-toggle-link-display
        "oop" 'org-latex-preview)


        
  ;; lsp-bridge
  (with-eval-after-load 'lsp-bridge
    (evil-define-key 'normal 'evil-normal-state-map
      "gd" 'lsp-bridge-find-def
      "gD" 'lsp-bridge-find-def-other-window
      "gr" 'lsp-bridge-find-references
      "ga" 'lsp-bridge-code-action
      "gh" 'lsp-bridge-popup-documentation)
    (define-key lsp-bridge-mode-map (kbd "M-J") 'lsp-bridge-popup-documentation-scroll-up)
    (define-key lsp-bridge-mode-map (kbd "M-K") 'lsp-bridge-popup-documentation-scroll-down))
    
  ;; conda 
  (define-leader-key 'normal 'global :localleader
                      "e" '(:wk "pyvenv")
                      "ea" 'conda-env-activate)


  ;; (with-eval-after-load 'dape
  ;;   (define-leader-key 'normal 'global nil
  ;;                      "d" '(:wk "dape")
  ;;                      "dd" 'dape
  ;;                      "dc" 'dape-continue
  ;;                      "dr" 'dape-restart
  ;;                      "dq" 'dape-disconnect-quit))
  (define-key evil-normal-state-map (kbd "C-<down>") 'evil-window-decrease-height)
  (define-key evil-normal-state-map (kbd "C-<up>") 'evil-window-increase-height)
  (define-key evil-normal-state-map (kbd "C-<left>") 'evil-window-decrease-width)
  (define-key evil-normal-state-map (kbd "C-<right>") 'evil-window-increase-width)

  (define-key evil-normal-state-map (kbd "M-j") 'evil-window-down)
  (define-key evil-normal-state-map (kbd "M-k") 'evil-window-up)
  (define-key evil-normal-state-map (kbd "M-h") 'evil-window-left)
  (define-key evil-normal-state-map (kbd "M-l") 'evil-window-right)

  (define-key evil-normal-state-map (kbd "M-d") 'evil-scroll-down)
  (define-key evil-normal-state-map (kbd "M-u") 'evil-scroll-up)
  (define-key evil-normal-state-map (kbd "C-k") '(lambda ()
                                                   (interactive)
                                                   (evil-previous-line 8)))
  (define-key evil-normal-state-map (kbd "C-j") '(lambda ()
                                                   (interactive)
                                                   (evil-next-line 8)))

  ;; (evil-define-key 'insert 'evil-insert-state-map (kbd "<tab>") 'forward-char)
  ;; (evil-define-key 'insert 'evil-insert-state-map (kbd "<backtab>" ) 'backward-char)

  (with-eval-after-load 'treemacs
    (define-key treemacs-mode-map (kbd "M-l") 'evil-window-right)
    (define-key treemacs-mode-map (kbd "M-h") 'evil-window-right)
    )

  (define-key evil-motion-state-map (kbd "C-a") nil)
  (define-key evil-motion-state-map (kbd "C-e") nil)
  (define-key evil-motion-state-map (kbd "M-a") nil)
  (define-key evil-motion-state-map (kbd "M-e") nil)

  (with-eval-after-load 'vterm
    (evil-define-key 'insert vterm-mode-map
                                    (kbd "C-c")'vterm--self-insert)
    )

  ;; evil-define-key 当使用-连接时，必须用kbd 
  (evil-define-key '(normal visual) 'global
    (kbd "M-e" )'treesit-end-of-defun
    (kbd "M-a")'treesit-beginning-of-defun)
  )

  (with-eval-after-load 'dired
 (define-key dired-mode-map (kbd "C-h") 'dired-up-directory)
 (define-key dired-mode-map (kbd "C-l") 'dired-find-file)
 )

(with-eval-after-load 'iedit
  (define-key iedit-mode-keymap (kbd "M-h") 'iedit-restrict-function)
  (define-key iedit-mode-keymap (kbd "M-i") 'iedit-restrict-current-line))

(global-set-key (kbd "s-o") 'toggle-chinese-search)

;; consult
(global-set-key (kbd "s-v") 'consult-yank-pop)

;; make c-j/c-k work in vertico selection
(with-eval-after-load 'vertico
  (define-key vertico-map (kbd "C-j") 'vertico-next)
  (define-key vertico-map (kbd "C-k") 'vertico-previous))

; org-agenda
(with-eval-after-load 'org-agenda
 (define-key org-agenda-mode-map (kbd "C-j") 'org-agenda-next-line)
 (define-key org-agenda-mode-map (kbd "C-k") 'org-agenda-previous-line)
 )


;; (evil-define-key 'normal 'evil-normal-state-map
;;   "" 'evil-window-decrease-height
;;   "<C-<up>" 'evil-window-decrease-width
;;   "<C-left>" 'evil-window-decrease-width
;;   "<C-right>" 'evil-window-increase-width)

(with-eval-after-load 'vertico
  (define-key vertico-map (kbd "C-j") 'vertico-next)
  (define-key vertico-map (kbd "C-k") 'vertico-previous)
)

(with-eval-after-load 'company
  (global-set-key (kbd "C-c C-/") #'company-other-backend)
  (global-set-key (kbd "C-x C-f") #'company-files)
  (global-set-key (kbd "C-x C-n") #'(lambda ()
                                      (interactive)
                                      (company-abort)
                                      (call-interactively 'company-dabbrev)))
  )

(with-eval-after-load 'lazy-load
  (with-eval-after-load 'org
    (define-key org-mode-map (kbd "C-'") nil)) ;; 占用
 (lazy-load-global-keys
  '(
    ("C-'" . vterm-toggle))
  "vterm-toggle")
 )


(provide 'init-keybinds)
;;; keybinds.el ends here
